package com.wtwd.campus.vcom.service.impl;

import com.wtwd.campus.dao.DeviceInfoMapper;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.vcom.common.RespCode;
import com.wtwd.campus.vcom.dao.VcomDeviceInfoMapper;
import com.wtwd.campus.vcom.service.ValidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/5 13:13
 * @Description:
 */
@Service
public class ValidServiceImpl implements ValidService {

    @Autowired
    RedisService redisService;
    @Autowired
    DeviceInfoMapper deviceInfoMapper;
    @Autowired
    VcomDeviceInfoMapper vcomDeviceInfoMapper;

    @Override
    public RespCode validSingleDevice(String token, String imei) {
        Map<String, Object> vcomUserInfo = redisService.getVcomUserInfo(token);
        if(vcomUserInfo == null || vcomUserInfo.isEmpty()){
            return RespCode.TOKEN_UNAUTHORIZED;
        }
        if(!token.equals(vcomUserInfo.get("accessToken"))){
            return RespCode.TOKEN_UNAUTHORIZED;
        }
        int customerId = Integer.parseInt((String)vcomUserInfo.get("customerId"));
        DeviceInfo deviceInfo = deviceInfoMapper.getDeviceInfoByImei(imei);
        if(deviceInfo == null){
            return RespCode.DEVICE_NOT_FOUND;
        }
        if(deviceInfo.getDeviceType() == 5 && deviceInfo.getCustomerId() == customerId){
            return RespCode.SUCCESS;
        }
        return RespCode.ILLEGAL_OPERATION;
    }

    @Override
    public Object validImeiList(String token, List<String> imeis) {
        Map<String, Object> vcomUserInfo = redisService.getVcomUserInfo(token);
        if(vcomUserInfo == null || vcomUserInfo.isEmpty()){
            return RespCode.TOKEN_UNAUTHORIZED;
        }
        if(!token.equals(vcomUserInfo.get("accessToken"))){
            return RespCode.TOKEN_UNAUTHORIZED;
        }
        int customerId = Integer.parseInt((String)vcomUserInfo.get("customerId"));
        List<String> resultImeis = vcomDeviceInfoMapper.getImeiListByImeiListAndCondition(imeis, 5, customerId);
        return resultImeis;
    }
}
